package flc.ast.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;

import android.app.Dialog;
import android.graphics.Color;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.bigkoo.pickerview.adapter.ArrayWheelAdapter;
import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.contrarywind.adapter.WheelAdapter;
import com.contrarywind.view.WheelView;

import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.adapter.CycleAdapter;
import flc.ast.bean.CycleBean;
import flc.ast.databinding.ActivityWorkTimeSetBinding;
import flc.ast.utils.AppSpUtils;
import flc.ast.utils.Content;
import stark.common.basic.utils.SPUtil;
import stark.common.basic.utils.ToastUtil;

public class WorkTimeSetActivity extends BaseAc<ActivityWorkTimeSetBinding> {

    private CycleAdapter cycleAdapter;
    private List<CycleBean> listShow = new ArrayList<>();
    private Dialog mySelTimeDialog;
    private WheelView wheelView1, wheelView2;
    private String goWorkTime, afterWorkTime;
    private int type;


    @Override
    protected int onCreate() {
        return R.layout.activity_work_time_set;
    }

    @Override
    protected void initView() {
        mDataBinding.ivWorkTimeSetBack.setOnClickListener(v -> onBackPressed());

        mDataBinding.tvGoWorkTime.setOnClickListener(this);
        mDataBinding.tvAfterWorkTime.setOnClickListener(this);
        mDataBinding.ivSave.setOnClickListener(this);

        mDataBinding.rvCycleList.setLayoutManager(new GridLayoutManager(mContext, 7));
        cycleAdapter = new CycleAdapter();
        mDataBinding.rvCycleList.setAdapter(cycleAdapter);
        cycleAdapter.setOnItemClickListener(this);

        //选择时间弹框
        selTimeDialog();
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.tvGoWorkTime://上班时间
                type = 0;
                mySelTimeDialog.show();
                break;
            case R.id.tvAfterWorkTime://下班时间
                type = 1;
                mySelTimeDialog.show();
                break;
            case R.id.ivSave://保存
                SPUtil.putString(mContext, Content.goWorkTime, goWorkTime);
                SPUtil.putString(mContext, Content.afterWorkTime, afterWorkTime);
                AppSpUtils.saveCycleList(listShow);
                ToastUtils.showShort(getString(R.string.save_success));
                finish();
                break;
            //选择时间弹框
            case R.id.ivDialogSelTimeCancel://取消
                mySelTimeDialog.dismiss();
                break;
            case R.id.ivDialogSelTimeRight://确定
                mySelTimeDialog.dismiss();
                int hour = wheelView1.getCurrentItem();
                int minute = wheelView2.getCurrentItem();
                String selTime = (hour < 10 ? "0" + hour : hour) + ":" + (minute < 10 ? "0" + minute : minute);
                if (type == 0) {
                    goWorkTime = selTime;
                    mDataBinding.tvGoWorkTime.setText(selTime);
                } else if (type == 1) {
                    afterWorkTime = selTime;
                    mDataBinding.tvAfterWorkTime.setText(selTime);
                }
                break;

        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        if (cycleAdapter.getItem(position).isSelected()) {
            cycleAdapter.getItem(position).setSelected(false);
        } else {
            cycleAdapter.getItem(position).setSelected(true);
        }
        cycleAdapter.notifyItemChanged(position);
    }

    @Override
    protected void initData() {
        listShow = AppSpUtils.getCycleList();
        cycleAdapter.setList(listShow);
        goWorkTime = SPUtil.getString(mContext, Content.goWorkTime, "09:00");
        afterWorkTime = SPUtil.getString(mContext, Content.afterWorkTime, "18:00");
        mDataBinding.tvGoWorkTime.setText(goWorkTime);
        mDataBinding.tvAfterWorkTime.setText(afterWorkTime);
    }

    //选择时间弹框
    private void selTimeDialog() {
        mySelTimeDialog = new Dialog(mContext, R.style.DialogStyle);
        View inflate = LayoutInflater.from(mContext).inflate(R.layout.dialog_sel_time, null);
        mySelTimeDialog.setContentView(inflate);
        //获取当前Activity所在的窗体
        Window dialogWindow = mySelTimeDialog.getWindow();
        //设置Dialog从窗体中心弹出
        dialogWindow.setGravity(Gravity.BOTTOM);
        //获得窗体的属性
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        DisplayMetrics dm = getResources().getDisplayMetrics();
        lp.width = (int) (dm.widthPixels * 1);
        // 将属性设置给窗体
        dialogWindow.setAttributes(lp);
        ImageView dialogCancel = inflate.findViewById(R.id.ivDialogSelTimeCancel);
        ImageView dialogRight = inflate.findViewById(R.id.ivDialogSelTimeRight);
        wheelView1 = inflate.findViewById(R.id.wheel_view1);
        wheelView2 = inflate.findViewById(R.id.wheel_view2);
        dialogCancel.setOnClickListener(this);
        dialogRight.setOnClickListener(this);
        wheelView1.setDividerColor(Color.parseColor("#2E979797"));
        wheelView1.setTextColorCenter(Color.parseColor("#000000"));
        wheelView1.setLineSpacingMultiplier(2.5f);
        wheelView1.setDividerType(WheelView.DividerType.WRAP);
        wheelView1.setCurrentItem(0);
        wheelView1.setItemsVisibleCount(5);
        wheelView1.setCyclic(true);
        wheelView1.setTextSize(18);
        wheelView2.setDividerColor(Color.parseColor("#2E979797"));
        wheelView2.setTextColorCenter(Color.parseColor("#000000"));
        wheelView2.setLineSpacingMultiplier(2.5f);
        wheelView2.setDividerType(WheelView.DividerType.WRAP);
        wheelView2.setCurrentItem(0);
        wheelView2.setItemsVisibleCount(5);
        wheelView2.setCyclic(true);
        wheelView2.setTextSize(18);
        //设置滚轮数据
        List<String> listHour = new ArrayList<>();//时
        for (int i = 0; i < 24; i++) {
            listHour.add(String.valueOf(i));
        }
        List<String> listMinute = new ArrayList<>();//分
        for (int i = 0; i < 60; i++) {
            listMinute.add(String.valueOf(i));
        }
        WheelAdapter adapter1 = new ArrayWheelAdapter(listHour);
        wheelView1.setAdapter(adapter1);
        WheelAdapter adapter2 = new ArrayWheelAdapter(listMinute);
        wheelView2.setAdapter(adapter2);
    }

}